// pages/home/home.js
const app = getApp()
var request = require('../../utils/request.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    searchValue:"",
    winWidth: 0,
    winHeight: 0,
    currentTab: 1,
    questionnaireTypes:[],
    votes:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;

    /**
     * 获取当前设备的宽高
     */
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }
    });
    //获取所有问卷类型
    this.getQuestionnaireType();
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getVotes();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //监听搜索的值
  searchChange: function (e) {
    console.log("搜索的值：" + e.detail.value);
    this.setData({
     searchValue: e.detail.value
    })
  },
  //搜索
  search(){
    this.setData({
      currentTab:1
    })
    //查询对应的问卷
    this.getVotes();
  },
  //  tab切换逻辑
  swichNav: function (e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current,
        searchValue:""
      })
      //查询对应的问卷
      that.getVotes();
    }
  },

  bindChange: function (e) {
    // var that = this;
    // that.setData({ currentTab: e.detail.current });

  },
  //获取所有的问卷类型
  getQuestionnaireType:function(){
    let that = this;
    request.getData(
      "?type=questionnaire_type",
      (data)=>{
        that.setData({
          questionnaireTypes:data.data
        })
      },
      (error)=>{

      }
    );
  },
  //获取所有的问卷
  getVotes:function(){
    let that = this;
    request.getData(
      "?type=vote&q_id=" + this.data.currentTab +"&keyword="+this.data.searchValue,
      (data) => {
        that.setData({
          votes: data.data
        })
      },
      (error) => {

      }
    );
  },
  addVote(e){
    console.log(e.currentTarget.dataset.id)
    //跳转到详情
    wx.navigateTo({
      url: '/pages/addvote/addvote?qid=' + e.currentTarget.dataset.id
    })
  },
  goVoteInfo: function (e) {
    let vId = e.currentTarget.dataset.vid;
    console.log(vId);
    //跳转到详情
    wx.navigateTo({
      url: '/pages/voteinfo/voteinfo?vId=' + vId
    })
  }
})